/**
 * Created by Adam on 2018/5/20 0:35.
 */

/**
 * 将来url参数转为json
 * @param url
 * @param type
 * @returns {{}}
 */
function urlToJson(url, type) {
    if (!type) {
        url = url == null ? window.location.href : url;
        var search = url.substring(url.lastIndexOf("?") + 1);
    }

    var obj = {};
    var reg = /([^?&=]+)=([^?&=]*)/g;
    search.replace(reg, function (rs, $1, $2) {
        var name = decodeURIComponent($1);
        var val = decodeURIComponent($2);
        val = String(val);
        obj[name] = val;
        return rs;
    });
    return obj;
}

/**
 * 成功信息提醒
 * @param msg
 * @param time
 */
function successMsg(msg, time) {
    alertMsg(msg, time, 0);
}

/**
 * 成功信息提醒
 * @param msg
 * @param time
 */
function errorMsg(msg, time) {
    alertMsg(msg, time, 1);
}

/**
 * 信息弹框
 * @param msg
 * @param time
 * @param type
 */
function alertMsg(msg, time, type) {
    var alert = 'alert-success';
    switch (type) {
        case 0:
            alert = 'alert-success';
            break;
        case 1:
            alert = 'alert-danger';
            break;
        case 2:
            alert = 'alert-info';
            break;
        case 3:
            alert = 'alert-warning';
            break;

    }
    var str = '<div style="display: none;position:fixed;left: 0;right: 0;top: 5%;margin:auto;width: fit-content;z-index: 9999999;padding: 6px 10px;opacity: 0.85;" class="msg-block alert ' + alert + '" role="alert">' + msg + '</div>';
    if ($(document).find('.msg-block').length > 0) {
        $(document).find('.msg-block').remove();
    }
    $(document).find('body').append(str);
    $(document).find('body .msg-block').fadeIn('100');
    $(document).find('body .msg-block').fadeOut(time ? time : 3000);
}

/**
 *
 * @param $fileObj
 * @param $imgPreviewObj
 */
function imgPreview(fileSelector,imgSelector) {
    $(fileSelector).on('change',function(){
        var file = this.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);//调用FileReader对象的方法
        reader.onloadend = function(e){
            $(imgSelector).attr({src:e.target.result});
        }
    });
}
